package com.coollang.smartbra.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.ActionDetailsActivity;
import com.coollang.smartbra.beans.ActionData;
import com.coollang.smartbra.utils.TimeUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Auther: Timmy
 * @Data: 2017/5/17
 * @Fuction: 平板支撑详情数据
 */

public class ActionDetailsAdapter1 extends RecyclerView.Adapter {

    private static final String TAG = "ActionDetailsAdapter1";
    private Context mContext;
    private List<Integer> mList;
    private ArrayList<ActionData> mActionDatas;

    private static final int VIEW_CORRECT_ITEM = 0;
    private static final int VIEW_ERROR_ITEM = 1;

    public ActionDetailsAdapter1(Context context) {
        mContext = context;
        mList = new ArrayList<>();
        mActionDatas = new ArrayList<>();
    }

    public void setList(List<Integer> list, ArrayList<ActionData> actionDatas) {
        mList = list;
        mActionDatas = actionDatas;
        // Collections.reverse(mActionDatas);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView;
        RecyclerView.ViewHolder holder = null;

        if (viewType == VIEW_CORRECT_ITEM) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_pbzc_details_corrent, parent, false);
            holder = new CorrectViewHolder(itemView);
        } else if (viewType == VIEW_ERROR_ITEM) {
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_pbzc_details_error, parent, false);
            holder = new ErrorViewHolder(itemView);
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);

        if (viewType == VIEW_CORRECT_ITEM) {
            ((CorrectViewHolder) holder).setData(position);
        } else if (viewType == VIEW_ERROR_ITEM) {
            ((ErrorViewHolder) holder).setData(position);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    //根据不同的数据返回不同的viewType
    @Override
    public int getItemViewType(int position) {
        int errorFlag = mActionDatas.get(getItemCount() - 1 - position).getErrorFlag();

        if (errorFlag == 1) {
            return VIEW_CORRECT_ITEM;
        } else {
            return VIEW_ERROR_ITEM;
        }
    }

    public ActionData getItem(int position) {
        return mActionDatas.get(position);
    }

    public int getItemPosition(ActionData actionData) {
        return mActionDatas.indexOf(actionData);
    }

    class CorrectViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_position)
        TextView mTvPosition;
        @BindView(R.id.item_div)
        View mLine;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_current_time)
        TextView mTvCurrentTime;

        private CorrectViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }

        public void setData(int position) {
            int itemCount = getItemCount();
            mTvPosition.setText(getTwoPosition(itemCount - position));

            int time = mList.get(itemCount - 1 - position);
            mTvTime.setText(getFormatTime(time));

            if (itemCount - 1 == position) {
                mLine.setVisibility(View.GONE);
            } else {
                time -= mList.get(itemCount - 2 - position);
                mLine.setVisibility(View.VISIBLE);
            }

            mTvCurrentTime.setText("+" + getFormatTime(time));
        }
    }

    class ErrorViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_position)
        TextView mTvPosition;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_currtime)
        TextView mTvCurrentTime;
        @BindView(R.id.tv_error_info)
        TextView mTvErrorInfo;

        private int mPosition;

        private ErrorViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
            ButterKnife.bind(this, itemView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((ActionDetailsActivity) mContext).onClickErrorItem(mPosition);
                }
            });
        }

        public void setData(int position) {

            mPosition = position;

            int itemCount = getItemCount();
            mTvPosition.setText(getTwoPosition(itemCount - position));

            ActionData actionData = mActionDatas.get(itemCount - 1 - position);
            mTvErrorInfo.setText(actionData.getErrorInfo());

            int time = mList.get(itemCount - 1 - position);
            mTvTime.setText(getFormatTime(time));

            time -= mList.get(itemCount - 2 - position);
            mTvCurrentTime.setText("+" + getFormatTime(time));
        }
    }

    // 设置引导数字

    private String getTwoPosition(int position) {

        if (position < 10) {
            return "0" + position;
        } else {
            return "" + position;
        }
    }

    private String getFormatTime(int time) {
        return TimeUtils.getFormatmillisecond(time);
    }
}
